
(function() {
    var config = require("config");
    var user = require("user");
    var item = require("item");
    var sellItem = require("sell_item");
    var dataManager = require("data_manager");
    var eventManager = require("event_manager");
    var Handler = {};

    Handler.getAccount = function() {
        var xmlRequest = function() {
            
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    var jsonObj = JSON.parse(this.responseText);
                    user.account = jsonObj.account;
                    console.log("user.account = " + user.account);
                }
            });
              
            xhr.open("GET", config.httpUrl + "getAccount", false);
              
            xhr.send(null);
        }
        xmlRequest();
    };

    Handler.createPlayer = function(nick, attibutes) {
        var xmlRequest = function(nick, attibutes) {
            
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {

                }
            });
              
            xhr.open("GET", config.httpUrl + "createPlayer?nick=" + nick + "&attibutes=" + attibutes, true);
              
            xhr.send(null);
        }
        xmlRequest(nick, attibutes);
    };

    Handler.getPlayer = function() {
        var xmlRequest = function() {
            
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "error") {
                    } else {
                        var jsonObj = JSON.parse(this.responseText);
                        if (jsonObj.valid) {
                            user.valid = jsonObj.valid;
                            user.nick = jsonObj.name;
                            user.power = jsonObj.power;
                            user.strength = jsonObj.strength;
                            user.wit = jsonObj.wit;
                            user.luck = jsonObj.luck;
                            user.itemList = jsonObj.store;
                            dataManager.updateStore(user.itemList);

                            console.log("[Handler]have got player");
                        } else {
                            console.log("[Handler]have not got player");
                            eventManager.fire(eventManager.PLAYER_CREATED_FAIL);
                        }
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "getPlayerInfo", false);
              
            xhr.send(null);
        }
        xmlRequest();
    };

    Handler.getItemInfo = function(tokenId) {
        var xmlRequest = function(tokenId) {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "error") {
                    } else {
                        var jsonObj = JSON.parse(this.responseText);
                        var itemObj = new item(jsonObj);
                        if (itemObj.valid) {
                            dataManager.updateItem(itemObj);
                        }
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "getItemInfo?tokenid="+tokenId, false);
              
            xhr.send(null);
        }
        xmlRequest(tokenId);
    };

    Handler.adventure = function(food, itemList) {
        var xmlRequest = function(food, itemList) {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "success") {
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "adventure?food=" + food + "&itemList=" + itemList, false);
              
            xhr.send(null);
        }
        xmlRequest(food, itemList);
    };

    Handler.getAdventureInfo = function() {
        var xmlRequest = function() {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "error") {
                    } else {
                        var jsonObj = JSON.parse(this.responseText);
                        var valid = jsonObj.valid;
                        var finish = jsonObj.finished;
                        var bagList = jsonObj.bagList;
                        var rewardItem = jsonObj.rewardItem;
                        if (valid) {
                            eventManager.fire(eventManager.EXPLORE_FINISH, bagList, rewardItem);
                        }
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "getAdventureInfo", false);
              
            xhr.send(null);
        }
        xmlRequest();
    };

    Handler.getAllTransactions = function() {
        var xmlRequest = function() {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "error") {
                    } else {
                        var jsonObj = JSON.parse(this.responseText);
                        var transIds = jsonObj.transIds;
                        if (transIds && transIds != "0") {
                            dataManager.updateMarketItems(transIds);
                        }
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "getAllTransactions", false);
              
            xhr.send(null);
        }
        xmlRequest();
    };

    Handler.getTransactionInfo = function(transid) {
        var xmlRequest = function(transid) {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                    if (this.responseText == "error") {
                    } else {
                        var jsonObj = JSON.parse(this.responseText);
                        var _sellItem = new sellItem(jsonObj);
                        dataManager.addMarketItems(_sellItem);
                    }
                }
            });
              
            xhr.open("GET", config.httpUrl + "getTransactionInfo?transid=" + transid, false);
              
            xhr.send(null);
        }
        xmlRequest(transid);
    };

    Handler.buyProduct = function(transid, weiValue) {
        var xmlRequest = function(transid, weiValue) {
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
              
            xhr.addEventListener("readystatechange", function () {
                if (this.readyState === this.DONE) {
                }
            });
              
            xhr.open("GET", config.httpUrl + "buyProduct?transid=" + transid + "&weiValue=" + weiValue, false);
              
            xhr.send(null);
        }
        xmlRequest(transid, weiValue);
    };
    

    module.exports = Handler;
})();